﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;

namespace MvcXRepairAdmin.Repository.SQL
{
    public class CityDAO : BaseDAO
    {
        public IEnumerable<City> GetAllActiveCities()
        {
            string spName = "GetAllActiveCities";

            DataTable resultTab = null;
            List<City> cityList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }
            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                cityList = this.ConvertDataTableToCityList(resultTab);
            }

            return cityList;

        }

        public bool DeactivateCityByID(int cityId)
        {
            string spName = "DeactivateCityByID";

            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", cityId);
                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool UpdateCity(City city)
        {
            string spName = "UpdateCity";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", city.ID);
                command.AddInParameter("StateID", city.StateID);
                command.AddInParameter("CityName", city.CityName);
                command.AddInParameter("LastUpdatedAt", city.LastUpdatedAt);
                command.AddInParameter("LastUpdatedBy", city.LastUpdatedBy);

                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool AddNewCity(City city)
        {
            string spName = "AddNewCity";
            object newIdentity = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("StateID", city.StateID);
                command.AddInParameter("CityName", city.CityName);
                command.AddInParameter("CreatedAt", city.CreatedAt);
                command.AddInParameter("CreatedBy", city.CreatedBy);

                command.AddOutParameter("NewIdentity", -1);
                command.ExecuteNonQuery();

                newIdentity = command.GetParameterValue("NewIdentity");
            }

            long newIdentityValue;

            if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
            {
                return true;
            }

            return false;
        }

        private List<City> ConvertDataTableToCityList(DataTable data)
        {
            List<City> cities = null;

            if (data != null)
            {
                cities = new List<City>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new City();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.StateID = SqlUtil<int>.GetValue(dr, "StateID");
                    serviceModel.CityName = SqlUtil<string>.GetValue(dr, "CityName");
                    serviceModel.IsActive = SqlUtil<bool>.GetValue(dr, "IsActive");
                    serviceModel.CreatedAt = SqlUtil<DateTime>.GetValue(dr, "CreatedAt");
                    serviceModel.CreatedBy = SqlUtil<String>.GetValue(dr, "CreatedBy");
                    serviceModel.LastUpdatedAt = SqlUtil<DateTime>.GetValue(dr, "LastUpdatedAt");
                    serviceModel.LastUpdatedBy = SqlUtil<string>.GetValue(dr, "LastUpdatedBy");

                    cities.Add(serviceModel);
                }
            }

            return cities;
        }
    }
}
